#!/usr/bin/python
#
# Copyright (C) 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = 'ales.kotnik@gmail.com (Ales Kotnik)'

from invoicecollector import InvoiceCollector
import getopt
import logging
import sys

def main():
  # test
  # parse command line options
  try:
    opts, args = getopt.getopt(sys.argv[1:], "", ["user=", "pw=", "sh="])
  except getopt.error, msg:
    print 'python processall.py --user [username] --pw [password] --sh [spreadsheet]'
    sys.exit(2)
  
  user = ''
  pw = ''
  sh = ''
  # Process options
  for o, a in opts:
    if o == "--user":
      user = a
    elif o == "--pw":
      pw = a
    elif o == "--sh":
      sh = a

  if user == '' or pw == '' or sh == '':
    print 'python processall.py --user [username] --pw [password] --sh [spreadsheet]'
    sys.exit(2)

  obj = InvoiceCollector(user, pw, sh)
  obj.fetchInvoices()
  xml=u"<?xml version='1.0' encoding='utf-8'?>\n<Invoices>"
  for invoice in obj.invoices:
    xml+=obj.invoices[invoice].toXml()
  xml+=u"</Invoices>"
  print xml.encode('utf-8')
  sys.exit(0)

if __name__ == '__main__':
  logging.root.setLevel(logging.DEBUG)
  main()

